# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.common.action_chains import ActionChains
import re
import json

"""

"""

# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待

    # WebDriverWait(driver, 50).until(
    #     EC.presence_of_element_located((By.ID, "nextpage2"))).click()

    # driver.find_element_by_xpath('//a[@class="ant-pagination-item-link"]').click()
    aa= re.findall(r"\"listOffer\":\[(.*?)\"relativeWords\"", driver.page_source, re.M|re.S)[0]
    data_json = json.loads(aa[:-2])
    selector = etree.HTML(driver.page_source)
    for s in selector.xpath('//article[@class="css-1dbjc4n r-1loqt21 r-1udh08x r-o7ynqc r-1j63xyz"]'):
        title = "".join(s.xpath('./div/div[2]/div[2]/div[2]//text()'))
        title += " " + "".join(s.xpath('./div/div[2]/div[2]/div[3]//text()'))
        print("========================")
        print(title)



    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "uname"))).send_keys("3007150014")
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "pwd"))).send_keys("Anfeng890602")
    # 点击下一页
    driver.find_element_by_xpath('//a[@class="inextpagea"]').click()
    # 点击下一页
    driver.find_element_by_id('login_btn').click()
    url = "http://elib.cugb.edu.cn/http/77726476706e69737468656265737421f1e7518f69276d52710e82a297422f30a0c6fa320a29ae/UA_GeneralSearch_input.do?product=UA&search_mode=GeneralSearch&SID=7BPanITJ9hPWsFRYBdw&preferencesSaved="
    driver.get(url)
    driver.find_element_by_id("value(input1)").clear()
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "value(input1)"))).send_keys("10.1103/PhysRevE.71.056608")
    driver.find_element_by_xpath('//button[@class="large-button primary-button margin-left-10"]').click()

    driver.find_element_by_xpath('//*[@id="RECORD_1"]/div[3]/div[1]/div/a').click()

    driver.back()


    selector = etree.HTML(html.text)
    xpath = selector.xpath('//div[@class="col-xs-12 col-sm-7 listing-description"]')
    for i in xpath:
        aa = i.xpath('./div/div/p//text()')
        bb = i.xpath('.ul[@class="listing-features"]/li//text()')
        cc = i.xpath('.ul[@class="listing-features pull-left"]/li//text()')
        pass

    xpath = html.xpath('//div[@class="txt-box"]')
    for x in html.xpath('//div[@class="txt-box"]'):
        title = ""
        titleUrl = ""
        titleContent = ""
        titleTime = ""
        titleFqr = ""
        for t in x.xpath('./h3/a//text()'):
            title += t
        titleUrl = x.xpath('./h3/a/@href')[0]
        for c in x.xpath('./p[@class="txt-info"]//text()'):
            titleContent += c
        titleFqr = x.xpath('./div[@class="s-p"]/a/text()')[0]
        titleTime = x.xpath('./div[@class="s-p"]/span/text()')[0]
    page = html.xpath('.//div[@class="p-fy"]/span')[0].text
    print(page)
    time.sleep(3)
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "sogou_next"))).click()
        # driver.find_element_by_id('sogou_next').click()
    except:
        close_driver(driver)



def close_driver(driver):
    driver.quit()


if __name__ == '__main__':
    url = "https://p4psearch.1688.com/p4p114/p4psearch/offer.htm?keywords=%E5%A4%A7%E6%9D%BF&cosite=baidujj_pz&location=re&trackid=%7Btrackid%7D&spm=a2609.11209760.j3f8podl.e5rt432e&keywordid=%7Bkeywordid%7D"
    driver = init_driver(url)
    get_url(driver)